package com.xyulu.controller.blog;

import com.xyulu.entity.BlogArticleEntity;
import com.xyulu.entity.BlogCategoryEntity;
import com.xyulu.entity.common.PageBean;
import com.xyulu.entity.vo.ArticleQuery;
import com.xyulu.service.BlogArticleService;
import com.xyulu.service.BlogCategoryService;
import com.xyulu.utils.ShiroUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author Xyulu
 * @Date 2021/3/26 21:20
 * @Version 1.0
 */
@Controller
public class ArticleController {

    @Resource
    private BlogCategoryService categoryService;
    @Resource
    private BlogArticleService articleService;

    @GetMapping("/articleList/{id}")
    public String showArticleByCondition(@PathVariable("id") Integer page,
                                         ArticleQuery query,
                                         HttpServletRequest request) {
        PageBean pageBean = new PageBean(page,"/articleList","");

        List<BlogArticleEntity> articles = articleService.showArticles(query,pageBean);
        List<BlogCategoryEntity> categories = categoryService.list();
        List<BlogArticleEntity> topViews = articleService.topViews();
        request.setAttribute("pageBean",pageBean);
        request.setAttribute("topViews",topViews);
        request.setAttribute("articles",articles);
        request.setAttribute("categories",categories);
        request.setAttribute("user",ShiroUtil.getLocalUserName());
        return "blog/article";
    }


    /**
     * 查看文章具体信息
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/article/{id}")
    public String findArticleById(@PathVariable("id") Integer id,HttpServletRequest request){

        BlogArticleEntity article = articleService.getById(id);
        request.setAttribute("article",article);
        request.setAttribute("user", ShiroUtil.getLocalUserName());
        return "blog/article_details";
    }

}
